package dyyx.zb;

import java.io.BufferedWriter;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

import dyyx.util.CommUtil;

public class EventDataGen {

	private static final Random RAND = new Random();

	private static final String SEP = ",";

	private static final String MIN_TIME_STR = "2020-01-01 00:00:00";
	private static final String MAX_TIME_STR = "2021-05-01 00:00:00";

	private static final long MIN_TIME_LONG = CommUtil.parseDate(MIN_TIME_STR, null).getTime();
	private static final long MAX_TIME_LONG = CommUtil.parseDate(MAX_TIME_STR, null).getTime();

	private static final int FACTOR = 100;
	private static final int MAX_INT_RANDOM = (int) ((MAX_TIME_LONG - MIN_TIME_LONG) / FACTOR - 1000);

	private static final AtomicLong SEQ = new AtomicLong(1);

	// event_id event_time account_from account_to amount trade_type

	public static void main(String[] args) throws Exception {

		int count = CommUtil.getInt(System.getProperty("count"));
		if (count <= 0) {
			count = 50000000;
		}
		String file = System.getProperty("file");
		if (CommUtil.isBlank(file)) {
			file = "/tmp/events.txt";
		}

		System.out.println("count=" + count + ",file=" + file);

		BufferedWriter writer = null;

		try {
			writer = CommUtil.createBufferedWriter(file, null, false);

			for (int i = 0; i < count; i++) {
				if(i>0 && i%1000000 <=0) {
					System.out.println(i+","+LocalDateTime.now());
				}
				writer.write(genDataLine());
				writer.newLine();
			}

		} finally {
			CommUtil.close(writer);
		}
		
		System.out.println("write done,"+LocalDateTime.now()+",count=" + count + ",file=" + file);


	}

	private static String genDataLine() {
		StringBuilder sb = new StringBuilder();
		sb.append(SEQ.getAndIncrement()).append(SEP);
		sb.append(genEventTime()).append(SEP);
		sb.append(genAcountFrom()).append(SEP);
		sb.append(genAcountTo()).append(SEP);
		sb.append(genAmount());

		return sb.toString();
	}

	private static int genAmount() {
		return 1 + RAND.nextInt(10000);
	}

	private static String genAcount(String prefix) {
		return prefix + (1000 + RAND.nextInt(100));
	}

	private static String genAcountFrom() {
		return genAcount("account_from_");
	}

	private static String genAcountTo() {
		return genAcount("account_to_");
	}

	private static String genEventTime() {
		long tmp = (long) RAND.nextInt(MAX_INT_RANDOM);
		long time = MIN_TIME_LONG + (tmp * FACTOR);
		Date date = new Date(time);
		return CommUtil.getDateString(date);
	}

	private static String genMaxEventTime() {
		long tmp = MAX_INT_RANDOM;
		long time = MIN_TIME_LONG + (tmp * FACTOR);
		Date date = new Date(time);
		return CommUtil.getDateString(date);
	}

}
